package com.younion.action.consumer.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.younion.action.consumer.AbstractRequestConsumer;
import com.younion.action.interactive.mobile.ApiConstant;
import com.younion.action.interactive.mobile.ResultObject;
import com.younion.business.IFavoritesService;
import com.younion.business.IMemberCardService;
import com.younion.common.constants.SysConstants;
import com.younion.model.MyFavoritesResult;
import com.younion.util.HttpRequestUtils;
import com.younion.util.JsonUtils;
import com.younion.util.config.ConfigHelper;
import com.younion.vo.API_CardEntityVO;
import com.younion.vo.API_MemberCardVO;
import com.younion.vo.FavoritesJoinCardEntityVO;
import com.younion.vo.FavoritesJoinFavourableEntityVO;

public class MyFavoritesListGetConsumer extends AbstractRequestConsumer {
	private IMemberCardService memberCardService;
	private IFavoritesService favoritesService;

	@Override
	public String execute(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		return getCardList(request, response);
	}

	public void setFavoritesService(IFavoritesService favoritesService) {
		this.favoritesService = favoritesService;
	}

	public String getCardList(HttpServletRequest request,
			HttpServletResponse response) {
		String user_token = request.getParameter("user_token");
		List<MyFavoritesResult> myFavoritesResultList = new ArrayList<MyFavoritesResult>();
		List<FavoritesJoinCardEntityVO> favoritesJoinCardEntityVOList = null;
		List<FavoritesJoinFavourableEntityVO> favoritesJoinFavourableEntityVOList = null;
		List<API_MemberCardVO> api_MemberCardVOList = null;
		ResultObject resultObject = new ResultObject();
		try {
			Map cardEntityMap = new HashMap();
			Map favourableEntityMap = new HashMap();
			cardEntityMap.put("userToken", user_token);
			cardEntityMap.put("type", 1);
			favourableEntityMap.put("userToken", user_token);
			favourableEntityMap.put("type", 2);
			api_MemberCardVOList = (List<API_MemberCardVO>) memberCardService
					.getMyCardByUserToken(user_token);
			favoritesJoinCardEntityVOList = (List<FavoritesJoinCardEntityVO>) favoritesService
					.getCardByMap(cardEntityMap);
			favoritesJoinFavourableEntityVOList = (List<FavoritesJoinFavourableEntityVO>) favoritesService
					.getFavourableByMap(favourableEntityMap);
			if (favoritesJoinCardEntityVOList != null
					&& favoritesJoinCardEntityVOList.size() > 0) {
				String card_img_url_pre = ConfigHelper.fastGetConfig("interface",
						"img_url_pre", "http://localhost:8080/younonmember/");
				for (FavoritesJoinCardEntityVO favoritesJoinCardEntityVO : favoritesJoinCardEntityVOList) {
					MyFavoritesResult myFavoritesResult = new MyFavoritesResult();
					myFavoritesResult.setFavour_id(favoritesJoinCardEntityVO
							.getFavourId().toString());
					myFavoritesResult.setName(favoritesJoinCardEntityVO
							.getCardEntityVO().getCardName());
					myFavoritesResult.setNonactivatedPic(card_img_url_pre
							+ SysConstants.CARD_IMG_URL + "/" + favoritesJoinCardEntityVO
							.getCardEntityVO().getId()
							+ "/" + favoritesJoinCardEntityVO
							.getCardEntityVO().getNonactivatedPic());
					myFavoritesResult.setActivatedPic(card_img_url_pre
							+ SysConstants.CARD_IMG_URL + "/" + favoritesJoinCardEntityVO
							.getCardEntityVO().getId()
							+ "/" + favoritesJoinCardEntityVO
							.getCardEntityVO().getActivatedPic());
					myFavoritesResult.setType(favoritesJoinCardEntityVO
							.getType().toString());
					myFavoritesResult.setApply("0");
					for (API_MemberCardVO aPI_MemberCardVO : api_MemberCardVOList) {
						if (aPI_MemberCardVO.getApi_CardEntityVO().getId() == favoritesJoinCardEntityVO
								.getCardEntityVO().getId()) {
							myFavoritesResult.setApply("1");
							break;
						}
					}
					myFavoritesResult.setAmount(favoritesJoinCardEntityVO
							.getCardEntityVO().getFavourAmount().toString());
					myFavoritesResultList.add(myFavoritesResult);
				}
			}
			if (favoritesJoinFavourableEntityVOList != null
					&& favoritesJoinFavourableEntityVOList.size() > 0) {
				String fav_img_url_pre = ConfigHelper.fastGetConfig("interface",
						"img_url_pre", "http://localhost:8080/younonmember/");
				for (FavoritesJoinFavourableEntityVO favoritesJoinFavourableEntityVO : favoritesJoinFavourableEntityVOList) {
					MyFavoritesResult myFavoritesResult = new MyFavoritesResult();
					myFavoritesResult
							.setFavour_id(favoritesJoinFavourableEntityVO
									.getFavourId().toString());
					myFavoritesResult.setName(favoritesJoinFavourableEntityVO
							.getFavourableEntityVO().getName());
					myFavoritesResult.setNonactivatedPic(fav_img_url_pre+ SysConstants.FavourableEntity_IMG_URL + 
							"/"+ favoritesJoinFavourableEntityVO
							.getFavourableEntityVO().getId() + "/"+favoritesJoinFavourableEntityVO
							.getFavourableEntityVO().getNonactivatedPic());
					myFavoritesResult.setActivatedPic(fav_img_url_pre+ SysConstants.FavourableEntity_IMG_URL + 
							"/"+ favoritesJoinFavourableEntityVO
							.getFavourableEntityVO().getId() + "/"+favoritesJoinFavourableEntityVO
							.getFavourableEntityVO().getActivatedPic());
					myFavoritesResult.setType(favoritesJoinFavourableEntityVO
							.getType().toString());
					myFavoritesResult.setApply("0");
					myFavoritesResult.setAmount(favoritesJoinFavourableEntityVO
							.getFavourableEntityVO().getFavourAmount()
							.toString());
					myFavoritesResultList.add(myFavoritesResult);
				}

			}
			resultObject.setResult(myFavoritesResultList);
			resultObject.setStatus(ApiConstant.SUCCESS_STATUS);

		} catch (Exception e) {
			resultObject.setStatus(ApiConstant.FALSE_STATUS);
			resultObject.setMsg(ApiConstant.PUBLIC_EXCEPTION);
			e.printStackTrace();
		}

		HttpRequestUtils.printContent(JsonUtils.parseObj2Json(resultObject),
				response);
		return null;
	}

	public void setMemberCardService(IMemberCardService memberCardService) {
		this.memberCardService = memberCardService;
	}
}
